﻿/**
 * 
 * Copyright 2009, 2010 Mike Jamer [email: mikejamer@gmail.com ]
 * 
 * This File is part of Moose IRC
 * 
 * MooseIRC is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * MooseIRC is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with MooseIRC.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using moose.irc;

namespace mooseirc.client.windowsforms.controls {
    /// <summary>
    /// Used to track Users in channel
    /// </summary>
    /// <remarks>
    /// Creating this control, kinda messy for now, 
    /// though don't really care, for now..
    /// </remarks>
    public partial class NickListControl
        : UserControl {
        #region [Local Attributes]

        /// <summary>
        /// Collection of users represented in this
        /// </summary>
        private List<IrcUser> m_users;

        /// <summary>
        /// Opped Users
        /// </summary>
        private List<IrcUser> m_op;

        //Voiced Users
        private List<IrcUser> m_voice;

        #endregion

        #region [Construction]

        /// <summary>
        /// Initialize Nick List
        /// </summary>
        public NickListControl()
            : base() {
            InitializeComponent();

            m_users = new List<IrcUser>();
            m_op = new List<IrcUser>();
            m_voice = new List<IrcUser>();

            return;
        }

        #endregion

        #region [Public Methods]

        /// <summary>
        /// Update User List
        /// </summary>
        /// <param name="channel">Channel to source all users from</param>
        public void update_userlist(IrcChannel channel) {
            //Clear collections first
            m_users.Clear();
            m_op.Clear();
            m_voice.Clear();

            //Next rebuild collections
            foreach (IrcUser user in channel.get_users()) {
                m_users.Add(user);
            }

            var modes = from m in channel.get_modes() where m.Mode == 'o' select m;
            if (modes.Count() > 0) //kinda gross, I know, I'll come up with a better way to describe this...
                foreach (string arg in modes.First().get_arguments()) {
                    m_op.Add((from u in m_users where u.Name == arg select u).First());
                }

            modes = from m in channel.get_modes() where m.Mode == 'v' select m;
            if (modes.Count() > 0)
                foreach (string arg in modes.First().get_arguments()) {
                    m_voice.Add((from u in m_users where u.Name == arg select u).First());
                }
            Invalidate();
            return;
        }

        #endregion

        #region [Overrides]

        /// <summary>
        /// Fires when painting
        /// </summary>
        /// <param name="e">Paint Event Args</param>
        protected override void OnPaint(PaintEventArgs e) {
            SortedList<int, string> users_print = new SortedList<int, string>();
            int op = 0;
            int voice = 1000;
            int reg = 2000;
            //Organize
            foreach (IrcUser user in m_users) {
                if (m_op.Contains(user)) {
                    users_print.Add(op++, "@" + user.Nickname);
                } else if (m_voice.Contains(user)) {
                    users_print.Add(voice++, "+" + user.Nickname);
                } else {
                    users_print.Add(reg++, "+" + user.Nickname);
                }
            }

            int x = 2,
                y = 0;
            using (Bitmap graphics_buffer = new Bitmap(this.Width, this.Height, e.Graphics)) {
                using (Graphics g = Graphics.FromImage(graphics_buffer)) {
                    g.Clear(BackColor);
                    g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
                    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

                    foreach (string nickname in users_print.Values) {
                        g.DrawString(nickname,
                            this.Font,
                            new SolidBrush(this.ForeColor),
                            x, y, StringFormat.GenericTypographic);
                        y += Font.Height + 2;
                    }

                    e.Graphics.DrawImageUnscaled(
                    graphics_buffer, 0, 0);
                }
            }
            return;
        }

        /// <summary>
        /// Fires on windows message
        /// </summary>
        /// <param name="m"></param>
        protected override void WndProc(ref Message m) {
            switch (m.Msg) {
                case Win32.WM_ERASEBKGND:
                    break;
                default:
                    base.WndProc(ref m);
                    break;
            };

        }

        #endregion

    }
}
